package com.lzk.auth.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzk.auth.service.ProductService;
import com.lzk.auth.service.TypeService;
import com.lzk.common.result.Result;
import com.lzk.model.system.SysProduct;
import com.lzk.model.system.SysType;
import com.lzk.vo.system.ShoppingVo;
import com.lzk.vo.system.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "产品管理")
@RestController
@RequestMapping("/admin/system/userproduct")
public class UserProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private TypeService typeService;

    @ApiOperation("设备分页查询")
    @GetMapping("{page}/{limit}/{ftype}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        @PathVariable Long ftype,
                        ProductVo productVo
    ) {
        Page<SysProduct> pageParam = new Page<>(page, limit);

        LambdaQueryWrapper<SysProduct> wrapper = new LambdaQueryWrapper<>();

        Integer ptype = productVo.getPtype();
        if (null != ptype) {
            wrapper.eq(SysProduct::getPType, ptype);
            System.out.println("-----------------");
            System.out.println(ptype);
        }
        String pname = productVo.getPname();
        if (!StringUtils.isEmpty(pname)) {
            wrapper.like(SysProduct::getPName, pname);
            System.out.println("-----------------");
            System.out.println(pname);
        }
        wrapper.eq(SysProduct::getPType, ftype);
        IPage<SysProduct> pageModel = productService.page(pageParam, wrapper);
        List<SysProduct> records = pageModel.getRecords();
        for (SysProduct record : records) {
            Integer pType = record.getPType();
            SysType type = typeService.getById(pType);
            record.setRoleName(type.getName());
        }
        return Result.ok(pageModel);
    }
    @ApiOperation(value = "根据id查产品")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysProduct product = productService.getById(id);
        return Result.ok(product);
    }
    @ApiOperation(value = "购买产品")
    @PostMapping("buy")
    public Result BuyProduct(@RequestBody ShoppingVo shoppingVo) {

        productService.order(shoppingVo);
        return Result.ok();
    }
}
